<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\jia;

use app\common\library\junziqian\Driver as DriverModel;
use app\common\model\jia\Contract as ContractModel;

/**
 * 文章分类模型
 * Class Category
 * @package app\store\model\article
 */
class Contract extends ContractModel
{

    public function getContractPdf()
    {
        $model = new DriverModel;

        $data['applyNo']      = $this['contract_no'];
        $data['fullName']     = $this['face_name'];
        $data['identityCard'] = $this['face_idcard'];
        $data['identityType'] = 1;

        $result = $model->getContractStatus($data);
        $status = 1;

        if (isset($result['data'])) {
            if ($result['data'] == 1 || $result['data'] == 3) {
                //获取合同链接
                $ss       = new DriverModel;
                $response = $ss->getLinkFile($data);
                $url      = $response['data'];
                $path     = "pdf/" . $this['order_id'] . ".pdf";
                $this->saveUrlToFile($url, $path);
                $this->save([
                    'path'         => $path,
                    'face_status'  => 2,
                    'contract_url' => $response['data'],

                ]);
                $status = 2;
            } else {

                $this->save([
                    'path'         => '',
                    'face_status'  => 3, //拒签
                    'contract_url' => '',

                ]);

            }

        }
        if ($this['contract_url'] != '') {
            $status = 2;
        }

        return $status;
    }

    public function saveUrlToFile($url, $filePath)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $data = curl_exec($ch);
        curl_close($ch);

        $success = file_put_contents($filePath, $data);
        return $success ? true : false;
    }

}
